package com.ld.admin.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.NoticeActionModel;
import com.ld.common.util.notice.NoticeManager;
import com.ld.common.util.notice.NoticeModel;
import com.ld.shieldsb.common.core.collections.ListUtils;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 通知动作配置
 * 
 * @ClassName NoticeActionController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年01月28日 15:02:22
 *
 */
@Api(tags = "通知动作配置")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "noticeaction.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemNoticeActionController")
@RequestMapping("${shieldsb.adminPath}/system/noticeaction")
public class NoticeActionController extends AdminBasicController<NoticeActionModel> {

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/noticeaction/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/noticeaction/noticeaction_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request, getModelClass());
        basicService.getPageBean(request, getModelClass(), queryModel);
        return "a/system/noticeaction/noticeaction_list";
    }

    @ApiOperation(value = "通知动作配置展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/system/noticeaction/noticeaction_show";
    }

    @ApiOperation(value = "跳转到添加/修改通知动作配置", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        setAttributes();
        long id = getLong("id");
        long pkey = getLong("pkey");
        String keyCode = getString("keyCode");

        Map<String, NoticeModel> noticesMap = NoticeManager.getAllNoticeModels();
        request.setAttribute("noticeTypes", noticesMap);
        if (StringUtils.isNotBlank(keyCode) && pkey != 0) { // 指定keyCode和pkey，由其他功能跳转过来的
            request.setAttribute("actionSet", "actionSet");
            QueryModel queryModel = new QueryModel();
            queryModel.addEq("pkey", pkey);
            queryModel.addEq("keyCode", keyCode);
            NoticeActionModel model = basicService.getOne(getModelClass(), queryModel);
            request.setAttribute("model", model); // 单个对象
        } else {
            basicService.getOne(request, getModelClass(), id);
        }
        return "a/system/noticeaction/noticeaction_update";
    }

    @ApiOperation(value = "添加/修改通知动作配置", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid NoticeActionModel model) {
        String type = model.getType();
        if (StringUtils.isNotBlank(type)) {
            String[] typeArrs = StringUtils.getStrArr(type);
            List<String> typeNameList = new ArrayList<>();
            for (String typeKey : typeArrs) {
                NoticeModel mocieModel = NoticeManager.get(typeKey);
                if (mocieModel != null) {
                    typeNameList.add(mocieModel.getName());
                }
            }
            if (ListUtils.isNotEmpty(typeNameList)) {
                model.setTypeName(String.join(",", typeNameList));
            }
        }
        Result result = basicService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "跳转到配置通知页面", notes = "")
    @RequestMapping(path = "/toSetNotice", method = RequestMethod.GET)
    public String toSetNotice() {
        setAttributes();
        long pkey = getLong("pkey");
        String keyCode = getString("keyCode");
        if (StringUtils.isNotBlank(keyCode) && pkey != 0) {
            request.setAttribute("actionSet", "actionSet");
        }
        QueryModel queryModel = new QueryModel();
        queryModel.addEq("pkey", pkey);
        queryModel.addEq("keyCode", keyCode);
        basicService.getOne(getModelClass(), queryModel);
        return "a/system/noticeaction/noticeaction_update";
    }

    @ApiOperation(value = "配置通知", notes = "")
    @RequestMapping(path = "/setnotice", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object setNotice(@Valid NoticeActionModel model) {
        Result result = basicService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除通知动作配置", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = basicService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

}